package com.ays.comm;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ays.bean.*;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author Administrator
 * @date 2024/6/5
 * @desc 平台字段映射转换
 */
public class Convert {
    // 科室字典
    public static AtmDeptDict covertAtmDeptDict(JSONObject json){
        AtmDeptDict bean = new AtmDeptDict();
        bean.setDeptCode(json.get("CTD_Code",String.class));
        bean.setDeptName(json.get("CTD_Desc",String.class));
        bean.setInputCode(json.get("CTD_Spell",String.class));
        return bean;
    }

    // 人员字典
    public static AtmHisUsers covertAtmHisUsers(JSONObject json){
        AtmHisUsers bean = new AtmHisUsers();
        bean.setUserId(json.get("CTCP_Code",String.class));
        bean.setUserName(json.get("CTCP_Desc",String.class));
        bean.setUserJob(json.get("CTCP_StaffType",String.class));
        bean.setUserDept(json.get("CTCP_DeptCode",String.class));
        bean.setCreateDate(json.get("CTCP_StartDate", Date.class));
        return bean;
    }

    // 手术字典
    public static AtmOperationDict covertAtmOperationDict(JSONObject json){
        AtmOperationDict bean = new AtmOperationDict();
        bean.setOperationCode(json.get("OperationCode",String.class));
        bean.setOperationName(json.get("OperationName",String.class));
        bean.setOperationScale(json.get("OperationClass",String.class));
        bean.setStdIndicator(json.get("StdIndicator",String.class));
        bean.setApprovedIndicator(json.get("ApprovedIndicator",String.class));
        bean.setCreateDate(json.get("CTOP_StartDate",Date.class));
        return bean;
    }

    // 手术预约申请
    public static AtmOperationSchedule covertAtmOperationSchedule(JSONObject json){
        AtmOperationSchedule bean = new AtmOperationSchedule();
        bean.setPatientId(json.get("PATPatientID",String.class));
        bean.setVisitId(json.get("PAADMVisitNumber",String.class));
        bean.setScheduleId(json.get("SCHEDULE_ID",String.class));
        bean.setDeptStayed(json.get("DEPT_STAYED",String.class));
        bean.setBedNo(json.get("BED_NO",String.class));
        bean.setScheduledDateTime(json.get("SCHEDULED_DATE_TIME",Date.class));
        bean.setOperatingRoomNo(json.get("OPERATING_ROOM_NO",String.class));
        bean.setSequence(json.get("SEQUENCE",String.class));
        bean.setDiagBeforeOperation(json.get("DIAG_BEFORE_OPERATION",String.class));
        bean.setPatientCondition(json.get("PATIENT_CONDITION",String.class));
        bean.setOperationScale(json.get("OPERATION_SCALE",String.class));
        bean.setEmergencyIndicator(json.get("EMERGENCY_INDICATOR",String.class));
        bean.setIsolationIndicator(json.get("ISOLATION_INDICATOR",String.class));
        bean.setOperatingDept(json.get("OPERATING_DEPT",String.class));
        bean.setSurgeon(json.get("SURGEON",String.class));
        bean.setFirstAssistant(json.get("FIRST_ASSISTANT",String.class));
        bean.setSecondAssistant(json.get("SECOND_ASSISTANT",String.class));
        bean.setThirdAssistant(json.get("THIRD_ASSISTANT",String.class));
        bean.setFourthAssistant(json.get("FOURTH_ASSISTANT",String.class));
        bean.setAnesthesiaMethod(json.get("ANESTHESIA_METHOD",String.class));
        bean.setAnesthesiaDoctor(json.get("ANESTHESIA_DOCTOR",String.class));
        bean.setSecondAnesthesiaDoctor(json.get("SECOND_ANESTHESIA_DOCTOR",String.class));
        bean.setThirdAnesthesiaDoctor(json.get("THIRD_ANESTHESIA_DOCTOR",String.class));
        bean.setAnesthesiaAssistant(json.get("ANESTHESIA_ASSISTANT",String.class));
        bean.setSecondAnesthesiaAssistant(json.get("SECOND_ANESTHESIA_ASSISTANT",String.class));
        bean.setThirdAnesthesiaAssistant(json.get("THIRD_ANESTHESIA_ASSISTANT",String.class));
        bean.setFourthAnesthesiaAssistant(json.get("FOURTH_ANESTHESIA_ASSISTANT",String.class));
        bean.setBloodTranDoctor(json.get("BLOOD_TRAN_DOCTOR",String.class));
        bean.setFirstOperationNurse(json.get("FIRST_OPERATION_NURSE",String.class));
        bean.setSecondOperationNurse(json.get("SECOND_OPERATION_NURSE",String.class));
        bean.setFirstSupplyNurse(json.get("FIRST_SUPPLY_NURSE",String.class));
        bean.setSecondSupplyNurse(json.get("SECOND_SUPPLY_NURSE",String.class));
        bean.setThirdSupplyNurse(json.get("THIRD_SUPPLY_NURSE",String.class));
        bean.setNotesOnOperation(json.get("NOTES_ON_OPERATION",String.class));
        bean.setReqDateTime(json.get("REQ_DATE_TIME",Date.class));
        bean.setEnteredBy(json.get("ENTERED_BY",String.class));
        bean.setState(json.get("STATUS",String.class));
        return bean;
    }

    // 病人基本信息
    public static AtmPatMasterIndex covertAtmPatMasterIndex(JSONObject json){
        AtmPatMasterIndex bean = new AtmPatMasterIndex();
        bean.setPatientId(json.get("PATPatientID",String.class));
        bean.setName(json.get("PATName",String.class));
        bean.setDateOfBirth(json.get("PATDob", Date.class));
        bean.setSex(json.get("PATSexCode",String.class));
        bean.setCitizenship(json.get("PATCountryCode",String.class));
        bean.setNation(json.get("PATNationDesc",String.class));

        JSONObject PATIdentityList = json.get("PATIdentityList", JSONObject.class);
        if(!ObjectUtils.isEmpty( PATIdentityList)){
            if(PATIdentityList.get("PATIdentity") instanceof JSONArray){
                JSONArray PATIdentitys = PATIdentityList.get("PATIdentity", JSONArray.class);
                if(!ObjectUtils.isEmpty(PATIdentitys)){
                    JSONObject PATIdentity = (JSONObject) PATIdentitys.get(0);
                    bean.setIdNo(PATIdentity.get("PATIdentityNum",String.class));
                    bean.setIdentity(PATIdentity.get("PATIdTypeDesc",String.class));
                }
            }else if(PATIdentityList.get("PATIdentity") instanceof JSONObject){
                JSONObject PATIdentity = PATIdentityList.get("PATIdentity", JSONObject.class);
                if(!ObjectUtils.isEmpty(PATIdentity)){
                    bean.setIdNo(PATIdentity.get("PATIdentityNum",String.class));
                    bean.setIdentity(PATIdentity.get("PATIdTypeDesc",String.class));
                }
            }
        }

        JSONObject PATAddressesList = json.get("PATAddressesList", JSONObject.class);
        if(!ObjectUtils.isEmpty(PATAddressesList)){
            if(PATAddressesList.get("PATAddress") instanceof JSONArray){
                JSONArray PATAddresses = PATAddressesList.get("PATAddress", JSONArray.class);
                if(!ObjectUtils.isEmpty(PATAddresses)){
                    JSONObject PATAddress = (JSONObject) PATAddresses.get(0);
                    bean.setMailingAddress(PATAddress.get("PATAddressDesc",String.class));
                    bean.setZipCode(PATAddress.get("PATPostalCode",String.class));
                }
            }else if(PATAddressesList.get("PATAddress") instanceof JSONObject){
                JSONObject PATAddress = PATAddressesList.get("PATAddress", JSONObject.class);
                if(!ObjectUtils.isEmpty(PATAddress)){
                    bean.setMailingAddress(PATAddress.get("PATAddressDesc",String.class));
                    bean.setZipCode(PATAddress.get("PATPostalCode",String.class));
                }
            }
        }

        JSONObject PATRelationList = json.get("PATRelationList", JSONObject.class);
        if(PATRelationList.get("PATRelation") instanceof JSONArray){
            JSONArray PATRelations = PATRelationList.get("PATRelation", JSONArray.class);
            if(!ObjectUtils.isEmpty(PATRelations)){
                JSONObject PATRelation = (JSONObject) PATRelations.get(0);
                bean.setNextOfKin(PATRelation.get("PATRelationName",String.class));
                bean.setRelationship(PATRelation.get("PATRelationDesc",String.class));
                bean.setNextOfKinPhone(PATRelation.get("PATRelationPhone",String.class));

                JSONObject PATRelationAddressList = PATRelation.get("PATRelationAddressList", JSONObject.class);
                if(PATRelationAddressList.get("PATRelationAddress") instanceof JSONArray){
                    JSONArray PATRelationAddresses = PATRelationAddressList.get("PATRelationAddress", JSONArray.class);
                    if(!ObjectUtils.isEmpty(PATRelationAddresses)){
                        JSONObject PATRelationAddress = (JSONObject) PATRelationAddresses.get(0);
                        bean.setNextOfKinAddr(PATRelationAddress.get("PATRelationAddressDesc",String.class));
                        bean.setNextOfKinZipCode(PATRelationAddress.get("PATRelationPostalCode",String.class));
                    }
                }else if(PATRelationAddressList.get("PATRelationAddress") instanceof JSONObject){
                    JSONObject PATRelationAddress = PATRelationAddressList.get("PATRelationAddress", JSONObject.class);
                    if(!ObjectUtils.isEmpty(PATRelationAddress)){
                        bean.setNextOfKinAddr(PATRelationAddress.get("PATRelationAddressDesc",String.class));
                        bean.setNextOfKinZipCode(PATRelationAddress.get("PATRelationPostalCode",String.class));
                    }
                }
            }

        }else if(PATRelationList.get("PATRelation") instanceof JSONObject){
            JSONObject PATRelation = PATRelationList.get("PATRelation", JSONObject.class);
            if(!ObjectUtils.isEmpty(PATRelation)){
                bean.setNextOfKin(PATRelation.get("PATRelationName",String.class));
                bean.setRelationship(PATRelation.get("PATRelationDesc",String.class));
                bean.setNextOfKinPhone(PATRelation.get("PATRelationPhone",String.class));
                JSONObject PATRelationAddressList = PATRelation.get("PATRelationAddressList", JSONObject.class);
                if(PATRelationAddressList.get("PATRelationAddress")instanceof JSONObject){
                    JSONObject PATRelationAddress = PATRelationAddressList.get("PATRelationAddress", JSONObject.class);
                    if(!ObjectUtils.isEmpty(PATRelationAddress)){
                        bean.setNextOfKinAddr(PATRelationAddress.get("PATRelationAddressDesc",String.class));
                        bean.setNextOfKinZipCode(PATRelationAddress.get("PATRelationPostalCode",String.class));
                    }
                }else if(PATRelationAddressList.get("PATRelationAddress")instanceof JSONArray){
                    JSONArray PATRelationAddresses = PATRelationAddressList.get("PATRelationAddress", JSONArray.class);
                    if(!ObjectUtils.isEmpty(PATRelationAddresses)){
                        JSONObject PATRelationAddress = (JSONObject) PATRelationAddresses.get(0);
                        bean.setNextOfKinAddr(PATRelationAddress.get("PATRelationAddressDesc",String.class));
                        bean.setNextOfKinZipCode(PATRelationAddress.get("PATRelationPostalCode",String.class));
                    }
                }
            }
        }

        bean.setPhoneNumberBusiness(json.get("PATWorkPlaceTelNum",String.class));
        bean.setPhoneNumberHome(json.get("PATTelephone",String.class));
        bean.setLastVisitDate(json.get("UpdateDate",Date.class));
        bean.setOperator(json.get("UpdateUserDesc",String.class));
        bean.setCreateDate(json.get("UpdateDate",Date.class));
        return bean;
    }

    // 住院登记附带的病人基本信息
    public static AtmPatMasterIndex covertAtmPatMasterIndexWithInHospital(JSONObject json){
        AtmPatMasterIndex bean = new AtmPatMasterIndex();
        bean.setPatientId(json.get("PATPatientID",String.class));
        bean.setName(json.get("PATName",String.class));
        bean.setDateOfBirth(json.get("PATDob", Date.class));
        bean.setSex(json.get("PATSexDesc",String.class));
        bean.setCitizenship(json.get("PATCountryCode",String.class));
        bean.setNation(json.get("PATNationDesc",String.class));
        bean.setIdNo(json.get("PATIdentityNum",String.class));
        bean.setIdentity(json.get("PATIdTypeDesc",String.class));
        bean.setPhoneNumberHome(json.get("PATTelephone",String.class));
        return bean;
    }

    // 住院登记信息
    public static AtmPatsInHospital covertAtmPatsInHospital(JSONObject json){
        AtmPatsInHospital bean = new AtmPatsInHospital();
        bean.setPatientId(json.get("PATPatientID",String.class));
        bean.setVisitId(json.get("PAADMVisitNumber",String.class));
        bean.setInpNo(json.get("PATDocumentNo",String.class));
        bean.setWardCode(json.get("PAADMAdmWardCode",String.class));
        bean.setDeptCode(json.get("PAADMAdmDeptCode",String.class));
        bean.setBedNo(json.get("PAADMCurBedNo",String.class));
        bean.setAdmissionDateTime(json.get("PAADMStartDate",Date.class));
        bean.setDoctorInCharge(json.get("PAADMAdmitDocCode",String.class));
        bean.setStartDateTime(json.get("PAADMStartDate",Date.class));

        JSONObject PAADMDiagnoseList = json.get("PAADMDiagnoseList", JSONObject.class);
        if(!ObjectUtils.isEmpty(PAADMDiagnoseList)){
            if(PAADMDiagnoseList.get("PAADMDiagnose") instanceof JSONObject){
                JSONObject PAADMDiagnose = PAADMDiagnoseList.get("PAADMDiagnose", JSONObject.class);
                if(!ObjectUtils.isEmpty(PAADMDiagnose)){
                    bean.setDiagnosis(PAADMDiagnose.get("PADDiagDesc",String.class));
                }
            }else if(PAADMDiagnoseList.get("PAADMDiagnose") instanceof JSONObject){
                JSONArray PAADMDiagnoses = PAADMDiagnoseList.get("PAADMDiagnose", JSONArray.class);
                if(!ObjectUtils.isEmpty(PAADMDiagnoses)){
                    JSONObject PAADMDiagnose = (JSONObject) PAADMDiagnoses.get(0);
                    bean.setDiagnosis(PAADMDiagnose.get("PADDiagDesc",String.class));
                }
            }
        }
        return bean;
    }

    // 手术名称
    public static AtmScheduledOperationName covertAtmScheduledOperationName(JSONObject json){
        AtmScheduledOperationName bean = new AtmScheduledOperationName();
        bean.setPatientId(json.get("PATPatientID",String.class));
        bean.setVisitId(json.get("PAADMVisitNumber",String.class));
        bean.setScheduleId(json.get("SCHEDULE_ID",String.class));
        bean.setOperation(json.get("OPERATION_Name",String.class));
        bean.setOperationCode(json.get("OPERATION_PositionCode",String.class));
        bean.setOperationScale(json.get("OPERATION_SCALE",String.class));
        bean.setOperationNo(json.get("OPERATION_NO",String.class));
        return bean;
    }
}
